package com.sunkyc.controller.system;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollectionUtil;
import com.sunkyc.commons.annotation.ReqLog;
import com.sunkyc.commons.entity.R;
import com.sunkyc.commons.exception.CustomerException;
import com.sunkyc.framework.system.entity.SysDictData;
import com.sunkyc.framework.system.service.SysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author sunkyc
 * @since 2022-05-31
 */
@RestController
@RequestMapping("/sysDictData")
@RequiredArgsConstructor
@Api(value = "字典数据前端控制器", tags = {"字典数据管理"})
public class SysDictDataController {
    private final SysDictDataService baseService;


    @GetMapping("/type/{dictType}")
    @ApiOperation("根据字典类型查询字典数据列表")
    @SaCheckPermission("system-dict-data-query")
    public R<List<SysDictData>> queryByType(@PathVariable("dictType") String dictType){
        List<SysDictData> data = baseService.queryByType(dictType);
        if(CollectionUtil.isEmpty(data)){
            data = new ArrayList<>();
        }
        return R.ok(data);
    }

    @PostMapping
    @ApiOperation("字典数据添加")
    @SaCheckPermission("system-dict-data-add")
    @ReqLog
    public R<Void> addDictData(@RequestBody @Validated SysDictData sysDictData){
        if(baseService.checkIsExistence(sysDictData)){
            throw  new CustomerException("添加 "+sysDictData.getDictLabel()+" 失败 数据字典数据已经存在");
        }
        return R.result(baseService.addDictData(sysDictData));
    }

    @PutMapping
    @ApiOperation("字典数据修改")
    @SaCheckPermission("system-dict-data-update")
    @ReqLog
    public R<Void> updateDictData(@RequestBody @Validated SysDictData sysDictData){
        return R.result(baseService.updateDictData(sysDictData));
    }


    @DeleteMapping("/{dictCode}")
    @ApiOperation("字典数据删除")
    @SaCheckPermission("system-dict-data-delete")
    @ReqLog
    public R<Void> deleteById(@PathVariable("dictCode") Long dictCode){
        return R.result(baseService.deleteById(dictCode));
    }



}
